﻿using System.Data;

namespace HenIT.Text
{
    /// <summary>
    /// DataTable formatting extensions
    /// </summary>
    public partial class HTMLWriter
    {
        #region DataTable formatting extensions
        /// <summary>
        /// Appends formatted DataTable to stream
        /// </summary>
        /// <param name="table">The DataTable that contains the data</param>
        /// <returns>Reference to current instance</returns>
        public HTMLWriter AppendDataTable(DataTable table)
        {
            return AppendDataTable(table, new DataTableFormatSettings(), "", new CustomAttribute[] { });
        }
        /// <summary>
        /// Appends formatted DataTable to stream plus formatting options
        /// </summary>
        /// <param name="table">The DataTable that contains the data</param>
        /// <param name="dataTableFormatSettings">The formatting options for the DataTable</param>
        /// <returns>Reference to current instance</returns>
        public HTMLWriter AppendDataTable(DataTable table, DataTableFormatSettings dataTableFormatSettings)
        {
            return AppendDataTable(table, dataTableFormatSettings, "", new CustomAttribute[] { });
        }
        /// <summary>
        /// Appends formatted DataTable to stream plus formatting options
        /// </summary>
        /// <param name="table">The DataTable that contains the data</param>
        /// <param name="dataTableFormatSettings">The formatting options for the DataTable</param>
        /// <param name="className">CSS class name for the displayed html table</param>
        /// <returns>Reference to current instance</returns>
        public HTMLWriter AppendDataTable(DataTable table, DataTableFormatSettings dataTableFormatSettings, string className)
        {
            return AppendDataTable(table, dataTableFormatSettings, className, new CustomAttribute[] { });
        }
        /// <summary>
        /// Appends formatted DataTable to stream plus formatting options
        /// </summary>
        /// <param name="table">The DataTable that contains the data</param>
        /// <param name="dataTableFormatSettings">The formatting options for the DataTable</param>
        /// <param name="className">CSS class name for the displayed html table</param>
        /// <param name="customAttributes">Custom html attributes for the html table</param>
        /// <returns>Reference to current instance</returns>
        public HTMLWriter AppendDataTable(DataTable table, DataTableFormatSettings dataTableFormatSettings, string className, params CustomAttribute[] customAttributes)
        {
            return AppendTableStart(className, customAttributes)
                .AppendDataTableColumns(table, dataTableFormatSettings)
                .AppendDataTableRows(table, dataTableFormatSettings)
                .AppendTagEnd();
        } 

        #region Private methods
        private HTMLWriter AppendDataTableColumns(DataTable table, DataTableFormatSettings dataTableFormatSettings)
        {
            AppendTableHeadStart().AppendTableRowStart();
            foreach (DataColumn col in table.Columns)
            {
                if (dataTableFormatSettings.DisplayLinkIDField || dataTableFormatSettings.LinkIDField != col.ColumnName)
                {
                    if (dataTableFormatSettings.LeftAlignHeadings)
                        AppendTableHeaderCellStart(dataTableFormatSettings.ColumnHeaderClass,
                        new CustomAttribute("align", "left"));
                    else
                        AppendTableHeaderCellStart(dataTableFormatSettings.ColumnHeaderClass);

                    AppendText(EscapeText(col.ColumnName))
                        .AppendTagEnd(); //cell
                }
            }
            AppendTagEnd(2);
            return this;
        }
        private HTMLWriter AppendDataTableRows(DataTable table, DataTableFormatSettings dataTableFormatSettings)
        {
            int alternatingIndex = 0;
            int maxAlternating = dataTableFormatSettings.RowClassNames.Length;
            bool displayLinks = (dataTableFormatSettings.LinkIDField.Length > 0 &&
                    dataTableFormatSettings.LinkPageUrl.Length > 0 &&
                    dataTableFormatSettings.LinkDisplayField.Length > 0);
            AppendTableBodyStart();
            foreach (DataRow r in table.Rows)
            {
                if (maxAlternating == 0)
                    AppendTableRowStart();
                else
                {
                    AppendTableRowStart(dataTableFormatSettings.RowClassNames[alternatingIndex]);
                    alternatingIndex++;
                    if (alternatingIndex >= maxAlternating)
                        alternatingIndex = 0;
                }

                foreach (DataColumn col in table.Columns)
                {
                    if (dataTableFormatSettings.DisplayLinkIDField || dataTableFormatSettings.LinkIDField != col.ColumnName)
                    {
                        if (displayLinks && dataTableFormatSettings.LinkDisplayField == col.ColumnName)
                        {
                            AppendTableCellStart()
                                .AppendAnchor(string.Format("{0}?{1}={2}",
                                   EscapeText(dataTableFormatSettings.LinkPageUrl),
                                    dataTableFormatSettings.LinkIDParameter,
                                    r[dataTableFormatSettings.LinkIDField].ToString()), EscapeText(r[col].ToString()))
                                .AppendTagEnd(); //cell
                        }
                        else
                            AppendTableCell(EscapeText(r[col].ToString()));
                    }
                }
                AppendTagEnd(); //row
            }
            AppendTagEnd(); //tbody
            return this;
        } 
        #endregion
        #endregion
    }
}
